package nth.bananas.game;

import java.util.ArrayList;
import java.util.Set;

import nth.bananas.solving.DefaultBoard;
import nth.bananas.solving.FakeSolver;
import nth.bananas.solving.Letter;
import nth.bananas.solving.Puzzle;
import nth.bananas.solving.Solver;

public class Player {

	private String name;

	private Puzzle tiles = new DefaultBoard(new ArrayList<Letter>());
	private Set<Puzzle> solutions;
	private Solver solve = new FakeSolver(this);
	GameMaster gm; // the game master this player is associated with

	public Player(GameMaster gm) {
		this.gm = gm;
	}

	public Player(GameMaster gm, String name) {
		this(gm);
		this.name = name;
	}
	
	public String toString() {
		return name == null ? super.toString() : name;
	}

	public void takeTile(Letter tile) {
		tiles.addFreeLetter(tile);		
	}
	
	public void submitPeelAction() {
		if (Game.isVerbose()) {
			System.out.printf("\t%s calls peel%n", this);
		}
		
		gm.postPeel(this);
	}
	
	public void peel() {
		if (Game.isVerbose()) {
			System.out.printf("\t%s peels. %d tiles remaining.%n", this, gm.bunchSize());
		}
		tiles.addFreeLetter(gm.emitRandom());
	}
	
	private boolean dump(Letter tile) {
		if (! gm.canTakeDump() || tiles.totalLetters() == 0) {
			return false;
		}
		
		tiles.removeFreeLetter(tile);
		gm.takeTile(tile);
		for (int i = 0; i < 3; i++) {
			tiles.addFreeLetter(gm.emitRandom());
		}
		return true;
	}
	
	// TODO: only for testing. gross.
	public boolean testDump() {
		Letter t = tiles.getRandomTile();
		return dump(t);
	}

	public void takeInitialPieces() throws RuntimeException {
		Letter piece = null;
		int startPieces = gm.bunchSize();
		for (int i = 0; i < gm.tilePerPlayer(); i++) {
			piece = gm.emitRandom();
			if (piece == null) {
				throw new RuntimeException(String.format("Game did not start with enough pieces to deal to all players (only %d)", startPieces));
			}
			tiles.addFreeLetter(piece);
		}
	}

	/**
	 * Do some unit of work towards completing the puzzle.
	 * Submits a peel action to the <code>GameMaster</code> if all the pieces are used.
	 */
	public void work() {
		solutions = solve.solve(tiles);
	}

	public int tileCount() {
		return tiles.totalLetters();
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((gm == null) ? 0 : gm.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result
				+ ((solutions == null) ? 0 : solutions.hashCode());
		result = prime * result + ((tiles == null) ? 0 : tiles.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Player other = (Player) obj;
		if (gm == null) {
			if (other.gm != null)
				return false;
		} else if (!gm.equals(other.gm))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (solutions == null) {
			if (other.solutions != null)
				return false;
		} else if (!solutions.equals(other.solutions))
			return false;
		if (tiles == null) {
			if (other.tiles != null)
				return false;
		} else if (!tiles.equals(other.tiles))
			return false;
		return true;
	}

}
